package com.zx.dsptuneup_TriMic.Adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zx.dsptuneup_TriMic.Base.BaseAdapter;
import com.zx.dsptuneup_TriMic.Data_Bean.MatrixVo;
import com.zx.dsptuneup_TriMic.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MatrixInputAdapter extends BaseAdapter {
    private Context context;
    private List<MatrixVo> gridViewBeanList;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private OnInputDbChangeListener onInputDbChangeListener;
    public List<MatrixVo> getGridViewBeanList() {
        return gridViewBeanList;
    }
    private String[] developArr = {"吊麦1","阵列麦1","3代扩声麦","阵列麦2","3代远程麦","阵列麦3","模拟桌麦","线路1","会议通道","啸叫返回","网络桌麦","保留1","保留2","保留3"};
    public void setOnDbChangeListener(OnInputDbChangeListener onDbChangeListener) {
        this.onInputDbChangeListener = onDbChangeListener;
    }

    public void setGridViewBeanList(List<MatrixVo> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        mSelectedPositions = new SparseBooleanArray();
        for (int i=0;i<gridViewBeanList.size();i++){
            setItemChecked(i, gridViewBeanList.get(i).getType()==1);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(context, R.layout.item_list_in_matrix,null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        MatrixVo childDeviceBean = gridViewBeanList.get(position);
        viewHolder.sel_check.setSelected(isItemChecked(position));
        viewHolder.db_rl.setSelected(isItemChecked(position));
        viewHolder.matrix_db.setText(childDeviceBean.getGain()+"");
        viewHolder.matrix_db.setFocusableInTouchMode(!isItemChecked(position));
        viewHolder.num_txt.setText((position+1)+"."+developArr[position]);
        viewHolder.matrix_db.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE) {
                    String searchTxt = viewHolder.matrix_db.getText().toString();

                    int type = isItemChecked(position) == true ? 1 : 0;
                    if(onInputDbChangeListener != null){
                        onInputDbChangeListener.InputDbChange(searchTxt,position,type);
                    }
                }
                return false;
            }
        });
        viewHolder.sel_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemChecked(position)) {
                    setItemChecked(position, false);
                } else {
                    setItemChecked(position, true);
                }
                notifyDataSetChanged();

                int type = isItemChecked(position) == true ? 1 : 0;
                if(onInputDbChangeListener != null){
                    onInputDbChangeListener.InputDbChange(viewHolder.matrix_db.getText().toString(),position,type);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return gridViewBeanList.size();
    }

    public MatrixInputAdapter(Context context, List<MatrixVo> gridViewBeanList) {
        super(context);
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    @Override
    public void notifyDataSetChanged(List dataList) {

    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    /**
     * 全选
     * */
    public void sellectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,true);
        }
        notifyDataSetChanged();
    }

    /**
     * 取消全选
     * */
    public void cancelSelectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,false);
        }
        notifyDataSetChanged();
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }
    //获得选中条目的结果
    public ArrayList<MatrixVo> getSelectedItem() {
        ArrayList<MatrixVo> selectList = new ArrayList<>();
        for (int i = 0; i < gridViewBeanList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(gridViewBeanList.get(i));
            }
        }
        return selectList;
    }

    public interface OnInputDbChangeListener {
        /**
         * 回调方法
         */
        void InputDbChange(String db,int position,int type);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.matrix_db_edt)
        EditText matrix_db;
        @BindView(R.id.sel_check)
        TextView sel_check;
        @BindView(R.id.num_txt)
        TextView num_txt;
        @BindView(R.id.db_rl)
        RelativeLayout db_rl;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
